<?php
class Model_ExpertsDynamic extends Model {
	
	protected $belongToTb = 'experts';

    /**
     *在读取每条数据后，将专家的id转为名称 
     * @param array &$row
     **/
	protected function _after_select_fetch(&$row){
		static $name=array();
		if($row['uid'] && !isset($row['name'])){
			if(!isset($name[$row['uid']])) {
				$row['_expert'] = M('Experts')->selectVar("name","where uid = {$row['uid']}");
				$name[$row['uid']] = $row['_expert'];
			}
			$row['_expert'] = $name[$row['uid']];
		}
		
	}
	public function save($data){
		$data['dateline'] = strtotime($data['dateline']);
		parent::save($data);
	}
	
	protected function _setIndexCondition(&$conditions,&$message){
		
		$search = _gp("search","array");
	
		$like = array(
			"name" => $search['name'],
		);

		$exact = array(
			"domain" =>  intval($search['domain']),
			"type" =>  	 intval($search['type']),
		);
		
		$this->assign("search",$search);

		$conditions = $this->phaseConditions($like,$exact,$date,$range);

		!empty($conditions) && $message = "搜索结果:";
	}
	
	function getListByCondition($conditionString){
		$tbname_item = $this->tbName($this->tbName);
		$tbname_experts = $this->tbName($this->belongToTb);
		
		$pars = parse_pars($conditionString);
	
		$condition = array();
		$data = array();
		
		if($pars['uid']){
			$condition[] = "i.uid in ({$pars['uid']})";
		}
		
		if($pars['grade']){
			$condition[] = "i.grade in ({$pars['grade']})";
		}
	
		if($pars['dateline']){
			$condition[] = "unix_timestamp() - i.dateline  < ({$pars['dateline']}) ";
		}
		
		if(count($condition)>0){
			$where =  ' and  ' . join(' and ', $condition);
		}
		
		if($pars['order']){
			$where .= " order by $pars[order] ";
		} else {
			$where .= " order by i.dateline desc ";
		}

		//$pars['perpage']必须先于$pars['limit']的条件处理
		if($pars['perpage']){
			$total = $this->selectCount(" where 1 $where");
			$pager = new Helper_Pager($total,$pars['perpage'],_gp('page','int'));
			$pars['limit'] = "{$pager->minimum} , {$pager->pagesize}";
			$data['pager'] = $pager->display;
		} 

		//$pars['limit']必须后于$pars['perpage']的条件处理
		if($pars['limit']){
			$where .= " limit $pars[limit] ";
		}
		
		$sql = "SELECT i.* FROM $tbname_item i 
		left join $tbname_experts e
		on i.uid = e.uid
		WHERE 1 $where";
		$list = $this->selectBySql($sql);
		
		foreach ($list as &$row){
			$row['subjectall'] = $row['subject'];
			if($pars['subjectlen']){
				$row['subject'] = _cutstr($row['subject'],$pars['subjectlen'],$pars['subjectdot']);
				if(!$pars['subjectdot'] && (substr($row['subject'], -1) == '.')){
					$row['subject'] = substr($row['subject'], 0, -1);
				}
			}
		}

		if($pars['perpage'] == ""){
			$data = $list;
		} else {
			$data['list'] = $list;
		}
		return $data;
	}
	/**
	 * 获取所有专家的动态数量
	 *
	 * @return $array
	 */
	function getExpertsDynamicQty()
	{
		
		$tbname_item = $this->tbName($this->tbName);
		//使用 uid as expert_id 避免进行关联表操作
		$data = $this->select("count(*) as dynamic_qty,uid as expert_id","group by uid");
		return Helper_Array::to_hashmap($data,'expert_id','dynamic_qty');
	}
	
}
?>